package cn.lefull.config;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @description:
 * @author: qijiang
 * @date: 2019/3/13 14:20
 */
@Aspect//切面注解
@Component//构成
public class RbacApartmentAspect {

    @Resource
    private TestService testService;
    //设置在ManagerController的login方法的切点
    @Around("within(cn.lefull.controller.TestController)")
    public String test1(ProceedingJoinPoint joinPoint) throws Throwable {
        {//切点映射，命名不规定
            Object aThis = joinPoint.getThis();
            Object[] args = joinPoint.getArgs();
            for (int i = 0; i < args.length; i++) {
                Class<?> aClass = args[i].getClass();
                JSONObject jsonObject = JSONObject.parseObject(JSON.toJSONString(args[i],SerializerFeature.WriteMapNullValue));
                if(jsonObject.containsKey("apartmentId") && StringUtils.isBlank(jsonObject.getString("apartmentId"))){
                    jsonObject.put("apartmentId",testService.addApartmentIds());
                }
                args[i] =JSONObject.parseObject(JSON.toJSONString(jsonObject),aClass);
            }
            return (String) joinPoint.proceed(args);
        }
//    //在本类的login执行之前
//    @Before("test1()")
//    public void beforelogin()
//    {
//        System.out.println("before");
//    }
//    //在本类的login执行之后执行
//    @After("test1()")
//    public void afterlogin(){System.out.println("after");}

    }
}
